/**
 * 部门管理初始化
 */
var Game = {
    id: "GameTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    matchType: 0
};

/**
 * 初始化表格的列
 */
Game.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '数据id', field: 'mid', visible: false, align: 'center', valign: 'middle'},
        {title: '比赛时间', field: 'time', align: 'center', valign: 'middle', sortable: true},
        {
            title: '比赛状态',
            field: 'expire',
            align: 'center',
            valign: 'middle',
            sortable: true,
            formatter: function (data) {
                if (data === '0') {
                    return '<div style="color:red;">已结束</div>';
                } else
                    return '<div style="color: #9ea6b9;">未结束</div>';
            },
        },
        {title: '联赛', field: 'title', align: 'center', valign: 'middle', sortable: true},
        {title: '主队', field: 'zhud', align: 'center', valign: 'middle', sortable: false},
        {title: '客队', field: 'ked', align: 'center', valign: 'middle', sortable: false},
        {
            title: '大下预测', field: 'dxResult', align: 'center', valign: 'middle', sortable: true,
            formatter: function (data) {
                if (data === '0') {
                    return '<div style="color:red;">失败</div>';
                } else if (data === '2') {
                    return '<div style="color:#aa00bb;font-weight:bold">大</div>';
                } else
                    return '<div style="color:#00b7ee;font-weight:bold">小</div>';
            },
        },
        {
            title: '胜负预测', field: 'sfResult', align: 'center', valign: 'middle', sortable: true,
            formatter: function (data) {
                if (data === '0') {
                    return '<div style="color:red;font-weight:bold">失败</div>';
                } else if (data === '2') {
                    return '<div style="color:#aa00bb;font-weight:bold">上盘</div>'
                } else
                    return '<div style="color: #550000;font-weight:bold">下盘</div>'
            },
        },
        {title: '轮次', field: 'lc', align: 'center', valign: 'middle', sortable: true},
        {
            title: '澳门亚盘', field: 'asianHandicap', align: 'center', valign: 'middle', sortable: false,
            formatter: function (data, row, index) {
                var asianHandicap = row.singleExpect.asianHandicap;
                if (asianHandicap === null) {
                    return '<div style="color:red;font-weight:bold">' + asianHandicap + '</div>'
                } else
                    return '<div>' + asianHandicap + '</div>';
            }
        },
        {
            title: '澳门大小球盘口', field: 'macaoHandicap', align: 'center', valign: 'middle', sortable: false,
            formatter: function (data, row, index) {
                var macaoHandicap = row.dxExpect.macaoHandicap;
                if (macaoHandicap === '') {
                    return '<div style="color:red;font-weight:bold">无</div>'
                } else
                    return '<div>' + macaoHandicap + '</div>';
            },
        },
        {title: '赔付率', field: 'pfl', align: 'center', valign: 'middle', sortable: false},
        {title: '实际比分', field: 'actualScore', align: 'center', valign: 'middle', sortable: false}
    ]
}
;

/**
 * 初始化表格行样式
 */
Game.rowStyle = function (row, index) {

    //这里有5个取值代表5中颜色['active', 'success', 'info', 'warning', 'danger'];
    var strClass = "";
    debugger
    if (row.dxResult < 2 && row.sfResult < 2) {
        strClass = 'warning'
    } else if (row.expire === '0') {
        if (row.dxExpect.forecast === row.dxExpect.result && row.singleExpect.forecast === row.singleExpect.result) {
            strClass = {'color': '#8cc657'}
        } else if (row.dxExpect.forecast === row.dxExpect.result || row.singleExpect.forecast === row.singleExpect.result) {
            strClass = 'success'
        } else {
            strClass = 'danger'
        }
    }
    else {
        return {};
    }
    return {classes: strClass}
};

/**
 * 检查是否选中
 */
Game.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length === 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Game.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加部门
 */
Game.view = function (param) {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '查看对阵记录',
            area: ['800px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/game/view/' + Game.seItem.mid + '?type=' + param
        });
        this.layerIndex = index;
    }
};

/**
 * 打开查看部门详情
 */
Game.viewStat = function () {
    if (this.check()) {
        debugger
        var index = layer.open({
            type: 2,
            title: '数据统计详情',
            area: ['800px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/game/viewStat/' + Game.seItem.mid
        });
        this.layerIndex = index;
    }
};


/**
 * 启动爬取任务
 */
Game.start = function () {

    var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/game/start", function () {
            Feng.success("启动成功!");
            Game.table.refresh();
        }, function (data) {
            Feng.error("启动失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type", Game.matchType);
        ajax.start();
    };
    Feng.confirm("是否启动爬虫任务?", operation);
};

/**
 * 启动更新任务
 */
Game.clear = function () {

    var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/game/clear", function () {
            Feng.success("更新成功!");
            Game.table.refresh();
        }, function (data) {
            Feng.error("更新失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Feng.confirm("是否更新当前数据?", operation);
};

/**
 * 查询部门列表
 */
Game.search = function () {
    var queryData = {};
    queryData['title'] = $("#title").val();
    queryData['expire'] = $("#expire").val();
    queryData['orders'] = $("#orders").val();

    Game.table.refresh({query: queryData});
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Game.formParams = function (params) {

    var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
        limit: params.limit,   //页面大小
        offset: params.offset,  //页码
        sort: params.sort,  //排序列名
        dxResult: $("#dxResult").val(),
        sfResult: $("#sfResult").val(),
        expire: $("#expire").val(),
        title: $("#title").val(),
        lc: $("#lc").val(),
        type: $("#matchType").val()
    };
    return temp;
};

$(function () {
    var defaultColunms = Game.initColumn();
    var table = new BSTable(Game.id, "/game/list", defaultColunms, Game.rowStyle);
    table.setQueryParams(Game.formParams);
    table.setPaginationType("client");
    table.setExport(true);
    table.setExportOptions({
        ignoreColumn: [0],  //忽略某一列的索引
        fileName: '单场预测表',  //文件名称设置
        worksheetName: 'sheet1',  //表格工作区名称
        tableName: '单场预测表',
        excelstyles: ['background-color', 'color', 'font-size', 'font-weight'],
    });
    Game.table = table.init();
    Game.matchType = $("#matchType").val();
});
